const fs = require('fs');
const path = require('path');

// 需要添加手动时间戳装饰器的实体文件列表
const entityFiles = [
    'nestjs-app/src/entities/user.entity.ts',
    'nestjs-app/src/entities/character.entity.ts',
    'nestjs-app/src/entities/character-attribute.entity.ts',
    'nestjs-app/src/entities/character-detailed-attribute.entity.ts',
    'nestjs-app/src/entities/character-skill.entity.ts',
    'nestjs-app/src/entities/character-religion.entity.ts',
    'nestjs-app/src/entities/character-residence.entity.ts',
    'nestjs-app/src/entities/character-stone.entity.ts',
    'nestjs-app/src/entities/character-resources.entity.ts',
    'nestjs-app/src/entities/character-inventory.entity.ts',
    'nestjs-app/src/entities/monthly-card.entity.ts',
    'nestjs-app/src/entities/character-location.entity.ts',
    'nestjs-app/src/entities/character-gem-inventory.entity.ts',
    'nestjs-app/src/entities/equipment-gem-socket.entity.ts',
    'nestjs-app/src/entities/character-attribute-points.entity.ts',
    'nestjs-app/src/entities/character-equipment.entity.ts',
    'nestjs-app/src/entities/character-training.entity.ts',
    'nestjs-app/src/entities/mount-training.entity.ts',
    'nestjs-app/src/entities/shop-goods.entity.ts',
    'nestjs-app/src/entities/faction-member.entity.ts',
    'nestjs-app/src/entities/gem-mining.entity.ts',
    'nestjs-app/src/entities/deputy-general-equipment.entity.ts',
    'nestjs-app/src/entities/deputy-general-stone.entity.ts',
    'nestjs-app/src/entities/announcement.entity.ts',
    'nestjs-app/src/entities/chat-message.entity.ts',
    'nestjs-app/src/entities/system-message.entity.ts',
    'nestjs-app/src/entities/item.entity.ts',
    'nestjs-app/src/entities/private-message.entity.ts',
    'nestjs-app/src/entities/basic-item.entity.ts',
    'nestjs-app/src/entities/equipment-base.entity.ts',
    'nestjs-app/src/entities/gem-base.entity.ts',
    'nestjs-app/src/entities/gem-attribute.entity.ts',
    'nestjs-app/src/entities/mount-base.entity.ts',
    'nestjs-app/src/entities/social-relation.entity.ts',
    'nestjs-app/src/entities/player-online-time.entity.ts',
    'nestjs-app/src/entities/map-area.entity.ts'
];

function addManualTimestampDecorator(filePath) {
    if (!fs.existsSync(filePath)) {
        console.log(`⏭️ 跳过不存在的文件: ${filePath}`);
        return;
    }

    let content = fs.readFileSync(filePath, 'utf8');
    let modified = false;

    // 检查是否已经有ManualTimestamp装饰器
    if (content.includes('@ManualTimestamp()')) {
        console.log(`⏭️ 跳过已添加装饰器的文件: ${filePath}`);
        return;
    }

    // 添加ManualTimestamp装饰器导入
    if (content.includes('import {') && content.includes('from "typeorm"')) {
        content = content.replace(
            /import \{([^}]+)\} from "typeorm";/,
            'import {$1} from "typeorm";\nimport { ManualTimestamp } from "../decorators/manual-timestamp.decorator";'
        );
        modified = true;
    }

    // 添加@ManualTimestamp()装饰器到类定义
    if (content.includes('@Entity(') && !content.includes('@ManualTimestamp()')) {
        content = content.replace(
            /@Entity\([^)]+\)\n@Unique\([^)]+\)\nexport class/,
            '@Entity($1)\n@Unique($2)\n@ManualTimestamp()\nexport class'
        );
        content = content.replace(
            /@Entity\([^)]+\)\nexport class/,
            '@Entity($1)\n@ManualTimestamp()\nexport class'
        );
        modified = true;
    }

    if (modified) {
        fs.writeFileSync(filePath, content);
        console.log(`✅ 添加手动时间戳装饰器: ${filePath}`);
    } else {
        console.log(`⏭️ 无需修改: ${filePath}`);
    }
}

console.log('🔧 开始为实体文件添加手动时间戳装饰器...');

entityFiles.forEach(addManualTimestampDecorator);

console.log('🎉 所有实体文件手动时间戳装饰器添加完成！');
